import axios from 'axios';

export default {
    data() {
        var validatePass = (rule, value, callback) => {
            if (value.value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.form1.passWD !== '') {
                    this.$refs.form1.validateField('passWD1');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value.value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value.value !== this.form1.passWD.value) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            form: {
                logonName: '',
                displayName: '',
                orgId: [],
                userState: ''
            },
            detailForm: {},
            dialogFormVisible: false,

            // data: generateData(),
            value: "",
            form1: {
                userId: {
                    value: ""
                },
                logonName: {
                    value: '',
                    p: ''
                },
                displayName: {
                    value: '',
                    p: ''
                },
                userType: {
                    key: '',
                    value: '',
                    p: ''
                },
                passWD: {
                    value: '',
                    p: ''
                },
                passWD1: {
                    value: '',
                    p: ''
                },
                orgId: {
                    key: '',
                    value: [],
                    p: ''
                },
                userExpireDate: {
                    value: '',
                    p: ''
                },
                pwExpireType: {
                    key: '',
                    value: '',
                    p: ''
                },
                pwExpireDate: {
                    value: '',
                    p: ''
                },
                cardType: {
                    key: '',
                    value: '',
                    p: ''
                },
                cardId: {
                    value: '',
                    p: ''
                },
                tel: {
                    value: '',
                    p: ''
                },
                department: {
                    key: '',
                    value: '',
                    p: ''
                },
                roleIds: {
                    value: []
                },
                aa26: {
                    value: []
                },
                aa26Tree: {
                    value: []
                }
            },
            rules1: {
                passWD: [
                    {validator: validatePass, trigger: 'blur'}
                ],
                passWD1: [
                    {validator: validatePass2, trigger: 'blur'}
                ],
                logonName: [{required: true}],
                displayName: [{required: true}]
            },
            codeTypes: {
                USERTYPE: [],
                PWEXPIRETYPE: [],
                CARDTYPE: [],
                DEPARTMENT: [],
                SYSORG: []
            },
            currentUserType: "",//当前登录用户类型
            tableData: {
                total: 0,
                size: 10,
                page: 0,
                data: [],
            },

            count: 1,
            departmentOptions: [],
            orgTreeData: [],//当前经办机构级联数据
            props: {
                label: 'orgname',
                value: 'orgid',
                children: 'children'
            },
            aa26TreeData: [],//区域树级联数据
            propsAa26: {
                label: 'aaa146',
                value: 'aab301',
                children: 'children'
            },
            selectedOptions: [],
            roleData: [],//角色穿梭框数据
            userTypeCode: [],//查询用户类型下拉框数据
            userState: [],//查询用户状态下拉框数据
            pitchData: [],//区域树默认选中节点
            aa26Disabled: false,
            orgIdDisabled: true
        }
    },
    computed: {
        init_form() {
            return this.$store.getters.init.form1;
        },
        doInitinit_form: {
            get: function () {
                if (this.$store.getters.common === null) return [];
                return this.$store.getters.common.form1;
            },
            set: function (newValue) {
                return newValue;
            }
        },
        backFun() {
            return this.$store.getters.backFun;
        }
    },
    watch: {
        init_form: {
            handler(value) {
                this.getInitForm(value);
            },
            deep: true
        },
        doInitform: {
            handler(value) {
                this.getInitForm(value);
            },
            deep: true
        },
        backFun: {
            handler(value) {
                var backFunType = value.backFunType;
                if (backFunType === "CHECK_LOGONNAME") {
                    this.form1.logonName.value = "";
                }
                if (backFunType === "SAVE_OK") {
                    this.dialogFormVisible = false;
                    this.form1.userId.value = "";
                    this.form1.logonName.value = "";
                    this.form1.displayName.value = "";
                    this.form1.userType.value = "";
                    this.form1.passWD.value = "";
                    this.form1.passWD1.value = "";
                    this.form1.orgId.value = [];
                    this.form1.userExpireDate.value = "";
                    this.form1.pwExpireType.value = "";
                    this.form1.pwExpireDate.value = "";
                    this.form1.cardType.value = "";
                    this.form1.cardId.value = "";
                    this.form1.tel.value = "";
                    this.form1.department.value = "";
                    this.form1.roleIds.value = [];
                    this.form1.aa26.value = [];
                    this.form1.aa26Tree.value = [];
                    this.queryUserList();
                }
            },
            deep: true
        }
    },
    mounted() {
        let param = {
            codeTypes: this.codeTypes,
            form1: this.form1,
            tableData: this.tableData
        };
        this.$store.dispatch('SET_INIT', param);
        var _this = this;  //很重要！！


        this.queryUserList();

        this.queryRoleList(0);

        this.$api.commonGet(this.$store.getters.path + "/getUserTypeCode").then(res => {
            if (res.status === "0") {
                _this.userTypeCode = res.data;
            }
        });

        this.$api.commonGet(this.$store.getters.path + "/getUserStateCode").then(res => {
            if (res.status === "0") {
                _this.userState = res.data;
            }
        });


    },
    methods: {
        qeuryOrgTreeData(areaIds) {
            let areaId
            if (Array.isArray(areaIds)) {
                areaId = areaIds[areaIds.length - 1];
            } else {
                areaId = areaIds;
            }

            let _this = this;
            axios.get(this.$route.path + '/getOrgTreeNodes/' + areaId).then(function (res) {
                _this.orgIdDisabled = false;
                _this.orgTreeData = res.data.data;
            }).catch(function (error) {
                console.log(error);
            });
        },

        handleCheckChange(data, checked, indeterminate) {
            console.log(data.aab301, checked, indeterminate);
            var dataAa26Tree = this.form1.aa26Tree.value;
            for (var i = 0; i < dataAa26Tree.length; i++) {

                if (dataAa26Tree[i].key == data.aab301) {
                    this.form1.aa26Tree.value.splice(i, 1);
                    return;
                }
            }
            if (!indeterminate) {
                this.form1.aa26Tree.value.push({key: data.aab301, value: checked});
            }
        },
        changeUserType(value) {
            let _this = this;
            if (value == '3') {
                _this.orgIdDisabled = true;
                _this.aa26Disabled = true;
            } else {
                _this.aa26Disabled = false;
            }
            if (value == '1' || value == '2') {//超级管理员或者区域管理员
                _this.queryAa26TreeNodes(value);
            }
            this.queryRoleList(value);
        },
        /**
         * 查询区域树数据
         * @param userType
         */
        queryAa26TreeNodes(userType) {
            let _this = this;
            axios.get(this.$route.path + '/getAa26TreeNodes/' + userType).then(function (res) {
                _this.aa26TreeData = res.data.data;
            }).catch(function (error) {
                console.log(error);
            });
        },
        /**
         * 查询角色列表
         * @param userType
         */
        queryRoleList(userType) {
            let _this = this;
            this.$api.commonGet(this.$store.getters.path + "/getRoleList/" + userType).then(res => {
                if (res.status === "0") {
                    console.log(res.data);
                    const data = [];
                    for (let i = 0; i < res.data.length; i++) {
                        data.push({
                            key: res.data[i].roleid,
                            label: res.data[i].rolename
                        });
                    }
                    _this.roleData = data;
                }
            });
        },

        selData(value) {
            this.userTypeCode = value;
        },

        /**
         * 获取初始
         */
        getInitForm(value) {
            this.form1 = value;
        },
        queryUserList(isQueryButton) {
            if (isQueryButton) {
                this.tableData.page = 1;
            }
            let orgId = "";
            if (this.form.orgId.length != 0) {
                orgId = this.form.orgId[this.form.orgId.length - 1];
            }
            const params = {
                logonName: this.form.logonName,
                displayName: this.form.displayName,
                orgId: orgId,
                userState: this.form.userState,
                page: this.tableData.page - 1,
                size: this.tableData.size
            };
            this.$api.commonGet(this.$store.getters.path + "/queryTable", params).then(res => {
                if (res.status === "0") {
                    this.tableData.total = res.data.totalElements;
                    this.tableData.data = res.data.content;
                }
            });
        },
        handleSizeChange(val) {
            this.tableData.size = val;
            if (this.tableData.total !== 0) {
                this.queryUserList();
            }
        },
        handleCurrentChange(val) {
            this.tableData.page = val;
            this.queryUserList();
        },
        /**
         * 获取初始
         */
        getInit(value) {
            this.form1 = value;
        },
        /**
         * 获取单条数据详情
         */
        getDetail(value) {
            this.detailForm = value;
        },
        /**
         * 编辑
         * @param id
         */
        handleEdit(id) {
            console.log(id);
            this.dialogFormVisible = true;
            let _this = this;
            this.$api.commonGet(this.$store.getters.path + "/queryOneUser/" + id).then(res => {
                if (res.status === "0") {
                    _this.form1.userId.value = res.data.sysUser.userId;
                    _this.form1.logonName.value = res.data.sysUser.logonName;
                    _this.form1.displayName.value = res.data.sysUser.displayName;
                    _this.form1.userType.value = res.data.sysUser.userType;
                    //_this.form1.passWD.value=res.data.sysUser.passWD;
                    //_this.form1.passWD1.value=res.data.sysUser.passWD1;
                    _this.form1.userExpireDate.value = res.data.sysUser.userExpireDate;
                    _this.form1.pwExpireType.value = res.data.sysUser.pwExpireType;
                    _this.form1.pwExpireDate.value = res.data.sysUser.pwExpireDate;
                    _this.form1.cardType.value = res.data.sysUser.cardType;
                    _this.form1.cardId.value = res.data.sysUser.cardId;
                    _this.form1.tel.value = res.data.sysUser.tel;
                    _this.form1.department.value = res.data.sysUser.department;

                    _this.queryAa26TreeNodes(res.data.sysUser.userType);
                    _this.qeuryOrgTreeData(res.data.sysUser.areaId);

                    if (res.data.sysOrg != null) {
                        var temOrgs = res.data.sysOrg.idpath.split("/");
                        for (var k = 0; k < temOrgs.length; k++) {
                            for (var j = 0; j < res.data.sysOrgs.length; j++) {
                                if (temOrgs != res.data.sysOrgs[j].orgid) {
                                    temOrgs.splice(k,1);
                                    k--;
                                } else {
                                    k = temOrgs.length;
                                    break;
                                }
                            }
                        }

                        for (var i = 0; i < temOrgs.length; i++) {
                            _this.form1.orgId.value.push(parseInt(temOrgs[i]));
                        }
                    }
                    if (res.data.aa26 != null) {
                        var temAa26 = res.data.aa26.idpath.split("/");
                        for (var i = 0; i < temAa26.length; i++) {
                            _this.form1.aa26.value.push(temAa26[i]);
                        }
                    }

                    let myArray = [];
                    for (var i = 0; i < res.data.areaList.length; i++) {
                        myArray.push(parseInt(res.data.areaList[i].aab301));
                    }
                    _this.pitchData = myArray;
                    for (var i = 0; i < res.data.roleList.length; i++) {
                        _this.form1.roleIds.value.push(res.data.roleList[i].roleId);
                    }


                }
            });
            //this.$store.dispatch('SET_DETAIL', id);
        },
        /**
         *注销用户
         * @param id
         */
        handleLogout(id) {
            console.log(id);
            let data = {userId: id};
            this.$confirm('此操作将注销用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                axios.post(this.$route.path + '/logoutUser', data).then(function (res) {
                    this.$message({
                        type: 'success',
                        message: '注销成功!'
                    });
                    this.queryUserList();
                }).catch(function (error) {
                    console.log(error);
                });

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消注销'
                });
            });
        },

        /**
         *重置密码
         * @param id
         */
        handleReset(id) {
            console.log(id);
            let data = {userId: id};
            this.$confirm('此操作将重置用户密码, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                axios.post(this.$route.path + '/resetPassWD', data).then(function (res) {
                    console.log(res);
                    this.$message({
                        type: 'success',
                        message: '重置成功!'
                    });
                }).catch(function (error) {
                    console.log(error);
                });

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消重置'
                });
            });
        },

        /**
         *解锁用户
         * @param id
         */
        handleUnlock(id) {
            console.log(id);
            let data = {userId: id};
            this.$confirm('此操作将解锁用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                axios.post(this.$route.path + '/unlockUser', data).then(function (res) {
                    this.$message({
                        type: 'success',
                        message: '解锁成功!'
                    });
                    this.queryUserList();
                }).catch(function (error) {
                    console.log(error);
                });

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消解锁'
                });
            });
        },

        /**
         * 提交表单
         */
        submit() {
            console.log(this.form1);
            // this.$store.dispatch('SET_FORM', this.form);
        },
        /**
         * 更新表单
         */
        update() {
            this.dialogFormVisible = false;
            let param = {
                form: this.detailForm,
                pager: this.pager
            };
            this.$store.dispatch('SET_FORMUPDATE', param);
        }
    }
}
